package com.czh.springboot04.service.serviceimpl;

import com.czh.springboot04.entity.MyTest;
import com.czh.springboot04.entity.PrivateMessage;
import com.czh.springboot04.entity.User;
import com.czh.springboot04.mapper.PrivateMessageMapper;
import com.czh.springboot04.mapper.UserMapper;
import com.czh.springboot04.service.PrivateMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PrivateMessageServiceImpl implements PrivateMessageService {

    @Autowired
    PrivateMessageMapper privateMessageMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public int insertPrivateMessage(PrivateMessage privateMessage) {
        int j = privateMessageMapper.insertPrivateMessage(privateMessage);
        if (j>0){
            return j;
        }
        return 0;
    }
    @Override
    public int updatePrivateMessageById(PrivateMessage privateMessage) {
        int i = userMapper.updateU(privateMessage.getUser());
        int j = privateMessageMapper.updatePrivateMessageById(privateMessage);
        if (i > 0 && j > 0) {
            return privateMessageMapper.updatePrivateMessageById(privateMessage);
        }
        return 0;
    }
    @Override
    public PrivateMessage selectPrivateMessageById(Integer pmId) {
        return privateMessageMapper.selectPrivateMessageById(pmId);
    }
    @Override
    public int deletePrivateMessageById(Integer pmId) {
        try {
           PrivateMessage privateMessage = privateMessageMapper.selectPrivateMessageById(pmId);
            if (privateMessage == null) {
                return 0;
            }
            int i = userMapper.deleteUser(privateMessage.getMemberId());
           int j = privateMessageMapper.deletePrivateMessageById(pmId);
            if (i > 0 && j > 0) {
                return j;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
    @Override
    public int deletePrivateMessageByList(List<Integer> pmIdList) {
        return privateMessageMapper.deletePrivateMessageByList(pmIdList);
    }
    @Override
    public PageInfo<PrivateMessage> selectPrivateMessageAll(Integer pmId, Integer memberId, Integer consultId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<PrivateMessage> privateMessages = privateMessageMapper.selectPrivateMessageAll(pmId, memberId, consultId);
        for (PrivateMessage privateMessage : privateMessages) {
            User user = userMapper.selectById(privateMessage.getMemberId());
            privateMessage.setUser(user);
        }
        return new PageInfo<>(privateMessages);
    }
}
